//
//  GifViewController.m
//  Ricky_RefreshControl
//
//  Created by MR-zhang on 15/7/9.
//  Copyright (c) 2015年 Ricky. All rights reserved.
//

#import "GifViewController.h"
#import "UIScrollView+RCKRefreshControl.h"
#import "RickyRefreshView.h"

/**
 * 随机色
 */
#define RickyRandomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]

static NSString *const RickyCollectionViewCellIdentifier = @"color";

@interface GifViewController ()
/** 存放假数据 */
@property (strong, nonatomic) NSMutableArray *colors;
@end

@implementation GifViewController
#pragma mark - 数据相关
- (NSMutableArray *)colors
{
    if (!_colors) {
        self.colors = [NSMutableArray array];
    }
    return _colors;
}

#pragma mark - 其他

/**
 *  初始化
 */
- (id)init
{
    // UICollectionViewFlowLayout的初始化（与刷新控件无关）
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(80, 80);
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    layout.minimumInteritemSpacing = 20;
    layout.minimumLineSpacing = 20;
    return [self initWithCollectionViewLayout:layout];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"GifRefresh";
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:RickyCollectionViewCellIdentifier];
    
    NSArray *images = @[[UIImage imageNamed:@"deliveryStaff0"],
                        [UIImage imageNamed:@"deliveryStaff1"],
                        [UIImage imageNamed:@"deliveryStaff2"],
                        [UIImage imageNamed:@"deliveryStaff3"]
                        ];
    
    __weak __typeof(self)weakSelf = self;
    
    // 添加头部刷新控件
    [self.collectionView addGifRickyRefreshHeaderViewWithPlaceHolderImage:[UIImage imageNamed:@"staticDeliveryStaff"] animationImageArray:images title:@"查询订单" withCallBackAction:^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        // 增加5条假数据
        for (int i = 0; i<10; i++) {
            [strongSelf.colors insertObject:RickyRandomColor atIndex:0];
        }
        
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [strongSelf.collectionView reloadData];
            
            // 结束刷新
            [strongSelf.collectionView completeRickyRefreshHeaderWithCompleteType:RickyRefreshCompleteTypeSuccess];
        });
    }];
    
    
    // 添加底部加载控件
    [self.collectionView addNormalRickyRefreshFooterViewWithCallBackAction:^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        // 增加5条假数据
        for (int i = 0; i<10; i++) {
            [strongSelf.colors insertObject:RickyRandomColor atIndex:0];
        }
        
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [strongSelf.collectionView reloadData];
            
            // 结束刷新
            [strongSelf.collectionView completeRickyRefreshFooterWithCompleteType:RickyRefreshCompleteTypeSuccess];
        });
    }];
    
}

#pragma mark - collection数据源代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.colors.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RickyCollectionViewCellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = self.colors[indexPath.row];
    return cell;
}

@end

